/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dqhrm.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.dqhrm.domain.DqDoctors;
import me.zhengjie.modules.dqhrm.service.DqDoctorsService;
import me.zhengjie.modules.dqhrm.service.dto.DqDoctorsQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.dqhrm.service.dto.DqDoctorsDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author Kuki Wu
* @date 2024-12-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "医生信息管理")
@RequestMapping("/api/dqDoctors")
public class DqDoctorsController {

    private final DqDoctorsService dqDoctorsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dqDoctors:list')")
    public void exportDqDoctors(HttpServletResponse response, DqDoctorsQueryCriteria criteria) throws IOException {
        dqDoctorsService.download(dqDoctorsService.queryAll(criteria), response);
    }

    @Log("导入数据")
    @ApiOperation("导入数据")
    @PostMapping("/import")
    public ResponseEntity<String> importDoctors(@RequestParam("file") MultipartFile file,
                                                @RequestParam("userId") Long userId) throws Exception {
        dqDoctorsService.importDoctors(file, userId);
        return ResponseEntity.ok("Import successful");
    }

    @GetMapping
    @Log("查询医生信息")
    @ApiOperation("查询医生信息")
    @PreAuthorize("@el.check('dqDoctors:list')")
    public ResponseEntity<PageResult<DqDoctorsDto>> queryDqDoctors(DqDoctorsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dqDoctorsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增医生信息")
    @ApiOperation("新增医生信息")
    @PreAuthorize("@el.check('dqDoctors:add')")
    public ResponseEntity<Object> createDqDoctors(@Validated @RequestBody DqDoctors resources){
        dqDoctorsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改医生信息")
    @ApiOperation("修改医生信息")
    @PreAuthorize("@el.check('dqDoctors:edit')")
    public ResponseEntity<Object> updateDqDoctors(@Validated @RequestBody DqDoctors resources){
        dqDoctorsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除医生信息")
    @ApiOperation("删除医生信息")
    @DeleteMapping
    @PreAuthorize("@el.check('user:del')")
    public ResponseEntity<Object> deleteDqDoctors(@RequestBody Set<Long> ids){
        dqDoctorsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}